package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.*;
import com.gtq.uhome.pojo.Delivery;
import com.gtq.uhome.pojo.OrderDetails;
import com.gtq.uhome.pojo.OrderSupRelation;
import com.gtq.uhome.service.DeliveryDetailsService;
import com.gtq.uhome.util.TimestampUtil;
import com.gtq.uhome.vo.DeliveryDetailsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 派送详情，待审核
 */
@Service("deliveryDetailsService")
@Transactional(propagation = Propagation. REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
public class DeliveryDetailsServiceImpl implements DeliveryDetailsService {

    @Resource
    private OrderDetailsMapper orderDetailsMapper;

    @Resource
    private DeliveryMapper deliveryMapper;

    @Resource
    private OrderSupRelationMapper orderSupRelationMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ConsigneeMapper consigneeMapper;

    @Resource
    private OrderMapper orderMapper;


    /**
     *  派送详情，审核成功/失败
     * @param deliveryDetailsVo
     * @return
     */
    @Override
    public List<DeliveryDetailsVo> findDeliveryDetails(DeliveryDetailsVo deliveryDetailsVo) {
        return orderDetailsMapper.findDeliveryDetails(deliveryDetailsVo);
    }

    /**
     * 派送详情，待审核，获取审核界面所有数据
     * @param deliveryDetailsVo
     * @return
     */
    @Override
    public List<DeliveryDetailsVo> findCheckPending(DeliveryDetailsVo deliveryDetailsVo) {
        return orderDetailsMapper.findCheckPending(deliveryDetailsVo);
    }

    /**
     *  派送详情，待审核，审核界面编辑更新
     * @param deliveryDetailsVos
     * @return
     */
    @Override
    public Boolean updateDeliveryDetails(List<DeliveryDetailsVo> deliveryDetailsVos) {
        Boolean aBoolean=false;
        // 遍历全部数据，分别取到各表数据
        for (DeliveryDetailsVo deliveryDetailsVo : deliveryDetailsVos) {
            List<Delivery> deliverys = deliveryDetailsVo.getDelivery();
            List<OrderDetails> orderDetails = deliveryDetailsVo.getOrderDetails();
            List<OrderSupRelation> orderSupRelations = deliveryDetailsVo.getOrderSupRelation();
            // 获得订单编号
            String orderId = deliveryDetailsVo.getOrderId();
            deliveryDetailsVo.setCheckTime(TimestampUtil.TimestampTime());
            Integer orderResult = orderMapper.update(deliveryDetailsVo);
            // 更新用户信息
            if (orderResult==1){
            Integer userResult = userMapper.updateDeliveryDetailsVo(deliveryDetailsVo);
            if (userResult==1){
                // 更新收货人地址信息
                Integer consigneeResult = consigneeMapper.updateDeliveryDetails(deliveryDetailsVo);
                if (consigneeResult==1){
                    // 遍历订单物料表，添加订单编号数据
                    for (OrderSupRelation orderSupRelation : orderSupRelations) {
                        orderSupRelation.setOrderId(orderId);
                        // 根据订单编号，更新订单物料表数据
                        Integer orderSupRelationResult = orderSupRelationMapper.updateAll(orderSupRelation);
                        if (orderSupRelationResult==1){
                            // 遍历订单详情表，分别更新订单详情表中的数据
                            for (OrderDetails orderDetail : orderDetails) {
                                // 更新订单详情表中的数据
                                Integer orderDetailsResult = orderDetailsMapper.update(orderDetail);
                                if (orderDetailsResult==1){
                                    // 遍历更新商品派送表中的信息
                                    for (Delivery delivery : deliverys) {
                                        Integer deliveryResult = deliveryMapper.updateAll(delivery);
                                        if (deliveryResult==1) {
                                            aBoolean = true;
                                        }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return aBoolean;
    }

}
